package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.cache.CacheKey;
import cn.e4j.core.cache.CacheUtil;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.RandNum;
import cn.e4j.core.util.SendTelMsgUtils;
import cn.e4j.modular.common.service.impl.SysAuthorityServiceImpl;
import cn.e4j.modular.crm.service.impl.GwdhTServiceImpl;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import cn.e4j.modular.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * 首页
 *
 * @author wenchuan.li
 * @date 2018/12/05 09:45
 */
@Controller
@RequestMapping("/admin")
@Slf4j
public class AdminController extends BaseController {
    @Autowired
    private GwdhTServiceImpl gwdhTServiceImpl;

    @Autowired
    private SysAuthorityServiceImpl sysAuthorityService;


    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 跳转到登录页面
     */

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return "/pc/system/admin.html";
    }

    //@RequiresPermissions("add000")
    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    @Cacheable(value = CacheUtil.TEMP, key = "'" + CacheKey.C_ADMIN_GETDATA + "'")
    @ResponseBody
    public ResponseData getData() {

        return SUCCESS_TIP;
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getNotices")
    @ResponseBody
    public ResponseData getNotices(@DtoParam DtoWapper dtoWapper) {
//        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWDHENT", "99");
        List<Dto> outDto = gwdhTServiceImpl.getNotices(inDto);
        return ResponseData.success(outDto);
    }



    /**
     * 查询未查看消息列表
     */
    @RequestMapping("/getGwdiList")
    @ResponseBody
    public ResponseData getGwdiList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser user = ShiroKit.getUser();
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getSmsCode")
    @ResponseBody
    public ResponseData getSmsCode(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        // controller中随机创建一个randNum
        String randNum = RandNum.createRandNum();
        System.out.println("验证码是：" + randNum);
        // randNum发送给手机号
        String result = SendTelMsgUtils.sendMsgTo(inDto.getString("cellphone"), randNum);
        System.out.println(result);
        outDto.put("smscode_back",randNum);
        return new SuccessResponseData(outDto);

    }
    @RequestMapping("/checkCellPhone")
    @ResponseBody
    public ResponseData checkCellPhone(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        Dto result = sysUserService.checkCellPhone(inDto);
        if(result.size()>0){
            if(result.getBigDecimal("num").intValue()>= new BigDecimal(1).intValue()){
                outDto.put("num",result.getBigDecimal("num"));
            }else {
                outDto.put("num",0);
            }
        }else {
            outDto.put("num",0);
        }
        return new SuccessResponseData(outDto);
    }

    @RequestMapping("/saveNewPassword")
    @ResponseBody
    public ResponseData saveNewPassword(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        inDto.put("password", ShiroKit.md5(inDto.getString("password")));
        sysUserService.saveNewPassword(inDto);
        return SUCCESS_TIP;
    }


    @RequestMapping("/getDigit")
    @ResponseBody
    public ResponseData getDigit(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        try {
            // 汇率小数位
            outDto.put("rate", Integer.parseInt(AppCxt.getParam("rate_digit_")));
            // 单价小数位
            outDto.put("price", Integer.parseInt(AppCxt.getParam("price_digit_")));
            // 金额小数位
            outDto.put("amount", Integer.parseInt(AppCxt.getParam("amount_digit_")));

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(outDto);
    }

}
